﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12

import "../components/"
import "../experiment"
import "../analysis"
import "../program"
import "../history"
import "../settings"
import "../maintain"
import "../help"
Window {


    id: mainwindow
    objectName: "mainwindow"

    flags: Qt.Window | Qt.window



    // C++信号监听
    Connections{
        target: CSignal
        onExperimentEnd: {          // Param(QString equId)
            var res = CSetUp.querySingleEquInfo(equId)
            showMsg("设备[" + res["EquName"] + "]的实验完成了，快去看看吧", 0)
        }
        onExperimentInterruption: { // Param(QString equId)
            var res = CSetUp.querySingleEquInfo(equId)
            showMsg("设备[" + res["EquName"] + "]的连接异常中断", 1)
        }
        onExperimentError: {
            var errorInfo = CExpPro.getExperimentErrorMsg(equId)
            var res = CSetUp.querySingleEquInfo(equId)
            showMsg("设备[" + res["EquName"] + "]发生异常: " + errorInfo, 1)
        }
    }
    Connections{
        target: CKeyEvent
        onCtrlPressed: {
            gKeyPressed_ctrl = true
        }onCtrlReleased: {
            gKeyPressed_ctrl = false
        }
    }

    onClosing:{
        // 判断当前是否有正在运行的设备
        let numList = CSetUp.queryEquNum()
        if(numList[3].toString() !== "0"){
            boxwarning_close.show()
            close.accepted = false
        }

    }
    BoxWarning{
        id: boxwarning_close
        box_text: "当前正在运行的实验将会丢失，确认关闭程序吗？"
        onAccept: {
            CSignal.exitProgram()
        }
    }

    Text{
        visible: false
        x: mainwindow.width - 180*dp; y: 15*dp
        text: "当前Ctrl状态:" + (gKeyPressed_ctrl ? "按下" : "抬起")
        color: "#EFEFEF"
        font.pixelSize: config.fontsize
    }
    onFocusObjectChanged: {
        if(activeFocusItem === null){
            gKeyPressed_ctrl = false
        }
    }

    property bool gKeyPressed_ctrl: false           // 全局键盘监听事件


    // 先把全局变量放这里了
    Config{
        id: config
    }
    function updateDPScale(){

        if(height < 900 || width < 1440){
            config.dpScale = Math.min(height/900, width/1440)
        }else{
            config.dpScale = 1.1
        }

    }

    onWidthChanged: {
        updateDPScale()
    }onHeightChanged: {
        updateDPScale()
    }

    property bool hasLoad: false
    property int tick: 0


    property int mainPage: 1
    property int targetPage: 1
    readonly property real dp: config.dp

    // 切换页面时，发送信号，进行一些传参和初始化
    signal tabProgram(var programName)
    signal tabSettings()
    signal tabExperiment()
    signal tabAnalysis(var expId, var expInfo)      // 从实验分析过来的时候需要传信息（为啥需要传信息来着……
    signal tabHistory()
    signal tabMaintain()
    signal tabHelp()


    // 请求切换页面
    signal askForTab()
    // 页面同意切换时调用
    function permitTab(){
        mainPage = targetPage
        if(targetPage === 1){
            tabExperiment()
        }else if(targetPage === 2){
            tabAnalysis(-1, -1)
        }else if(targetPage === 3){
            tabHistory()
        }else if(targetPage === 4){
            tabProgram(-1)
        }else if(targetPage === 5){
            tabMaintain()
        }else if(targetPage === 6){
            tabSettings()
        }else if(targetPage === 7){
            tabHelp()
        }
    }

    Component.onCompleted: {
        initTimer.start()
        updateDPScale()
    }

    Timer{
        id: initTimer           // 初始化界面刷新
        interval: 100
        repeat: false
        onTriggered: {
            console.log("======tabExperiment")
            tabExperiment()
        }
    }

    visible: true
    width: 1440*dp
    height: 900*dp
    minimumWidth: 1440
    minimumHeight: 900
    title: qsTr("RT-PCR-96_Analysis_software (Multi-machine)")

    Row{
        x: 10*dp
        y: 5*dp
        height: 40*dp
        width: mainwindow.width - 20*dp
        spacing: 5*dp
        PushButton{
            id: button1
            height: 40*dp
            title: "运行实验"
            button_icon: "qrc:/picture/tabExperiment.png"
            button_icon_select: "qrc:/picture/tabExperiment_select.png"
            isSelected: mainPage === 1
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 1
                askForTab()
            }
        }
        PushButton{
            id: button2
            height: 40*dp
            title: "数据分析"
            button_icon: "qrc:/picture/tabAnalysis.png"
            button_icon_select: "qrc:/picture/tabAnalysis_select.png"
            isSelected: mainPage === 2
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 2
                askForTab()
            }
        }
        PushButton{
            id: button3
            height: 40*dp
            title: "实验数据"
            button_icon: "qrc:/picture/tabHistory.png"
            button_icon_select: "qrc:/picture/tabHistory_select.png"
            isSelected: mainPage === 3
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 3
                askForTab()
            }
        }
        PushButton{
            id: button4
            height: 40*dp
            title: "扩增程序"
            button_icon: "qrc:/picture/tabProgram.png"
            button_icon_select: "qrc:/picture/tabProgram_select.png"
            isSelected: mainPage === 4
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 4
                askForTab()
            }
        }
        PushButton{
            id: button5
            height: 40*dp
            title: "维护"
            button_icon: "qrc:/picture/tabMaintain.png"
            button_icon_select: "qrc:/picture/tabMaintain_select.png"
            isSelected: mainPage === 5
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 5
                askForTab()
            }
        }
        PushButton{
            id: button6
            height: 40*dp
            title: "设置"
            button_icon: "qrc:/picture/tabSettings.png"
            button_icon_select: "qrc:/picture/tabSettings_select.png"
            isSelected: mainPage === 6
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 6
                askForTab()
            }

        }
        PushButton{
            id: button7
            height: 40*dp
            title: "帮助"
            button_icon: "qrc:/picture/tabHelp.png"
            button_icon_select: "qrc:/picture/tabHelp_select.png"
            isSelected: mainPage === 7
            button_color_select: "#F0F0F0"
            text_color_select: "#1677FF"
            onReleased: {
                targetPage = 7
                askForTab()
            }
        }
    }


    Rectangle{
        height: mainwindow.height - 55*dp
        width: mainwindow.width - 10*dp
        x: 5*dp
        y: 50*dp

        color: "#F7F7FA"

        PageExperiment{
            visible: mainPage === 1
        }PageAnalysis{
            visible: mainPage === 2
        }PageHistory{
            visible: mainPage === 3
        }PageProgram{
            visible: mainPage === 4
        }PageMaintain{
            visible: mainPage === 5
        }PageSettings{
            visible: mainPage === 6
        }PageHelp{
            visible: mainPage === 7
        }


    }




    // 提示弹窗
    function showToast(msg, type){
        toast.show(msg, type)
    }
    function showMsg(msg, type){
        msgManager.show(msg, type)
    }


    MessageManager{
        id: msgManager
        y: 50*dp
    }

    Toast{
        id: toast
        y: 100*dp
        x: parent.width / 2 - 150*dp
    }


}
